package cn.xzc.job.cocurrent;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

/**
 * 异步回调接口【提交任务之后，在任务执行结束之后；自动触发通知给调用者】
 *
 * @author xzc
 * 当前时间 2024-02-12 21:30:00
 */
public interface ListenableFuture<T> extends Future<T> {

    /**
     * Register the given {@code ListenableFutureCallback}.
     *
     * @param callback the callback to register
     */
    void addCallback(ListenableFutureCallback<? super T> callback);

    /**
     * Java 8 lambda-friendly alternative with success and failure callbacks.
     *
     * @param successCallback the success callback
     * @param failureCallback the failure callback
     */
    void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback);


    /**
     * Expose this {@link ListenableFuture} as a JDK {@link CompletableFuture}.
     */
    default CompletableFuture<T> completable() {
        CompletableFuture<T> completable = new DelegatingCompletableFuture<>(this);
        addCallback(completable::complete, completable::completeExceptionally);
        return completable;
    }

}